﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_Common;
using Nova_MOD;
using Nova_BLL;
using Nova_DAL;
using System.Data.SqlClient;
using System.Data.Common;
using Nova_Common.Extend;

public partial class Views_NoTray : System.Web.UI.Page
{
    DbHelper access = new DbHelper();
    TCommandBll CmdBll = new TCommandBll();
    DataSet ds = null;
    DataSet dss = null;
    DbCommand cmd = null;
    string strSQL = "";
    string lblNo_Line = "";
    DbDataReader dbdr = null;
    DataTable myTable = null;
    DataSet dswcy = null;
    string cid = "";        ////层
    int rid = 0;        //gridview行的编号
    int iLcol = 0;
    string lbl_LaneNo = ""; //巷道号
    int iLineNo = 0;               //排
    int iLayNo = 0;           //层
    int colArea = 60;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlbind();
            bind();

        }

    }

    protected void bind()
    {
        int subArea = int.Parse(ddlNo_Col.SelectedValue);
        myTable = DTableTBase_Position(ddlNo_Lane.SelectedValue.ToString(),subArea);
        if (myTable != null && myTable.Rows.Count > 0)
        {
            GV.DataSource = myTable;
            GV.DataBind();
        }
    }


    public DataTable DTableTBase_Position(string lbl_LaneNo,int subArea)
    {
        DataTable myTable = new DataTable("mytable");
        lblNo_Line = " [ " + lbl_LaneNo.ToString() + " ] ";
        try
        {
            myTable.Columns.Add(lblNo_Line, System.Type.GetType("System.String"));

            myTable.Columns.Add("巷道", System.Type.GetType("System.String"));
            //myTable.Columns.Add("1", System.Type.GetType("System.String"));
            strSQL = "Select No_Col from  TBase_Position where SubArea="+subArea+" and No_Lane=" + lbl_LaneNo + " group by No_Col order by No_Col";
            cmd = access.GetSqlStringCommond(strSQL);
            dbdr = access.ExecuteReader(cmd);
            while (dbdr.Read())
            {
                myTable.Columns.Add(dbdr["No_Col"].ToString(), System.Type.GetType("System.String"));
            }
            dbdr.Close();
            dbdr.Dispose();


            strSQL = "select No_Line ,No_Layer from TBase_Position where SubArea=" + subArea + " and No_Lane=" + lbl_LaneNo + " group by No_Line,No_Layer order by No_Line ,No_Layer";
            cmd = access.GetSqlStringCommond(strSQL);
            ds = access.ExecuteDataSet(cmd);
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = myTable.NewRow();
                    dr[lblNo_Line] = ds.Tables[0].Rows[i]["No_Line"].ToString() + "排";

                    dr["巷道"] = ds.Tables[0].Rows[i]["No_Layer"].ToString() + "层";


                    strSQL = "Select No_Col,No_Line ,No_Layer,Pos_State as Pos_State,PID from TBase_Position where SubArea=" + subArea + " and NO_Line='" + ds.Tables[0].Rows[i]["No_Line"].ToString() + "' and No_Layer = '" + ds.Tables[0].Rows[i]["No_Layer"].ToString() + "' and No_Lane=" + lbl_LaneNo + " order by No_Col";

                    cmd = access.GetSqlStringCommond(strSQL);
                    dss = access.ExecuteDataSet(cmd);
                    if (dss.Tables[0].Rows.Count > 0)
                    {
                        for (int j = 0; j < dss.Tables[0].Rows.Count; j++)
                        {

                            if (dss.Tables[0].Rows[j]["Pos_State"].ToString() == "0")//无货
                            {
                                dr[dss.Tables[0].Rows[j]["No_Col"].ToString()] = "";
                            }
                            else if (dss.Tables[0].Rows[j]["Pos_State"].ToString() == "1")//有货
                            {
                                strSQL = @"SELECT TRP.TRAY_ID,TRP.POS_ID,TBP.NO_LINE,TBP.NO_COL,TBP.NO_LAYER,TBP.PID 
		                                    FROM TRelations TRP
		                                    LEFT OUTER JOIN TBASE_TRAYPOS TBT ON TRP.TRAY_ID=TBT.TRAY_ID
		                                    LEFT OUTER JOIN TREL_TRAYPOSCONTAINER TRT ON TBT.RELATIONS_ID=TRT.RELATIONS_ID
		                                    LEFT OUTER JOIN TBASE_STORAGE TBS ON TRT.CONTAINERID=TBS.CONTAINERID
		                                    LEFT OUTER JOIN TBASE_POSITION TBP ON TRP.POS_ID=TBP.POS_ID
		                                    WHERE TBS.PID IS NULL AND TBP.NO_LANE=@LaneNo AND TRP.Pos_ID != '200000000000' AND TBP.PID = '" + dss.Tables[0].Rows[j]["PID"].ToString() + "'";
                                cmd = access.GetSqlStringCommond(strSQL);
                                access.AddInParameter(cmd, "@LaneNo", DbType.String, lbl_LaneNo);
                                dswcy = access.ExecuteDataSet(cmd);

                                if (dswcy.Tables[0].Rows.Count > 0)
                                {
                                    dr[dss.Tables[0].Rows[j]["No_Col"].ToString()] = "◇";//空托盘
                                }
                                else
                                {
                                    dr[dss.Tables[0].Rows[j]["No_Col"].ToString()] = "◆";//有货
                                }
                            }
                            else
                            {
                                dr[dss.Tables[0].Rows[j]["No_Col"].ToString()] = "¤";
                            }
                        }
                    }
                    myTable.Rows.Add(dr);
                }
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.ToString());
        }
        return myTable;
    }


    protected void ddlNo_Lane_SelectedIndexChanged(object sender, EventArgs e)
    {
        bind();
    }

    protected bool reInt(string strNum)
    {
        try
        {
            int var1 = Convert.ToInt32(strNum);
            return true;
        }
        catch
        {
            return false;
        }
    }


    protected void ddlbind()
    {
        strSQL = "select No_Lane,'第'+convert(varchar(50),No_Lane)+'巷道' as No_Lane_Text from TLaneDefine order by No_Lane";
        cmd = access.GetSqlStringCommond(strSQL);
        ds = access.ExecuteDataSet(cmd);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            ddlNo_Lane.DataSource = ds;
            ddlNo_Lane.DataValueField = "No_Lane";
            ddlNo_Lane.DataTextField = "No_Lane_Text";
            ddlNo_Lane.DataBind();
        }

        //让前端绑定 
        //strSQL = "SELECT SQIDE FROM ECSStandard..TSQLanePath WHERE TaskMode =2 Group by SQIDE ORDER BY SQIDE";
        //cmd = access.GetSqlStringCommond(strSQL);
        //ddlSQLIDE.DataSource = access.ExecuteDataSet(cmd);
        //ddlSQLIDE.DataBind();
        //ddlSQLIDE.Items.Insert(0, new ListItem("设备选择", "0"));

    }
    protected void ibtnSelect_Click(object sender, ImageClickEventArgs e)
    {
        if (txtSLine.Text != "" && txtSLcol.Text != "")
        {
            if (!reInt(txtSLine.Text))
            {
                //ShowBox.MessageBox(this.Page, "开始坐标请输入数字!");   //此方法不适用于 UpdatePanel下的提示         
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "提示", "alert(' 开始坐标请输入数字!')", true);
                return;
            }
            if (!reInt(txtSLcol.Text))
            {
                //ShowBox.MessageBox(this.Page, "开始坐标请输入数字!");
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "提示", "alert(' 开始坐标请输入数字!')", true);
                return;
            }
            if (txtELine.Text != "" && txtELine.Text != "")
            {
                if (!reInt(txtELine.Text))
                {
                    //ShowBox.MessageBox(this.Page, "结束坐标请输入数字!");
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "提示", "alert(' 结束坐标请输入数字!')", true);
                    return;
                }
                if (!reInt(txtELcol.Text))
                {
                    //ShowBox.MessageBox(this.Page, "结束坐标请输入数字!");
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "提示", "alert(' 结束坐标请输入数字!')", true);
                    return;
                }

                int SLine = Convert.ToInt32(txtSLine.Text.ToString());
                int ELine = Convert.ToInt32(txtELine.Text.ToString());
                int iLine = 0;

                int SLcol = Convert.ToInt32(txtSLcol.Text.ToString());
                int ELcol = Convert.ToInt32(txtELcol.Text.ToString());
                int iLcol = 0;

                if (SLine > ELine)
                {
                    txtSLine.Text = ELine.ToString();
                    txtELine.Text = SLine.ToString();
                    iLine = SLine;
                    SLine = ELine;
                    ELine = iLine;

                }

                if (SLcol > ELcol)
                {
                    txtSLcol.Text = ELcol.ToString();
                    txtELcol.Text = SLcol.ToString();
                    iLcol = SLcol;
                    SLcol = ELcol;
                    ELcol = iLcol;
                }

                for (int x = SLine; x <= ELine; x++)
                {
                    for (int j = SLcol; j <= ELcol; j++)
                    {
                        //GV.Rows[x - 1].Cells[j].BackColor = System.Drawing.Color.FromName("#ff0000");     //列号从2开始
                        GV.Rows[x - 1].Cells[j + 1].BackColor = System.Drawing.Color.FromName("#ff0000"); //列号从1开始
                    }
                }
            }
            else
            {
                GV.Rows[Convert.ToInt32(txtSLine.Text) - 1].Cells[Convert.ToInt32(txtSLcol.Text)].BackColor = System.Drawing.Color.FromName("#ff0000"); //列号从2开始
                //GV.Rows[Convert.ToInt32(txtSLine.Text) - 1].Cells[Convert.ToInt32(txtSLcol.Text) + 1].BackColor = System.Drawing.Color.FromName("#ff0000"); //列号从1开始
            }
        }
    }
    protected void ibtnReset_Click(object sender, ImageClickEventArgs e)
    {
        txtELcol.Text = "";
        txtELine.Text = "";
        txtSLcol.Text = "";
        txtSLine.Text = "";
        lab_Msg.Text = "";
        bind();
    }

    protected void GV_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Attributes["style"] = "width:30px;";
            cid = e.Row.Cells[1].Text.ToString().Substring(0, 1);
            rid = e.Row.RowIndex + 1;
            lbl_LaneNo = ddlNo_Lane.SelectedValue.ToString();       //巷道号
            iLineNo = Convert.ToInt32(e.Row.Cells[0].Text.Substring(0, 1));     //排
            //iLcol = e.Row.RowIndex;         //列
            //iLayNo = Convert.ToInt32(e.Row.Cells[1].Text.Substring(0, 1));     //层
            string strLayNo = e.Row.Cells[1].Text;
            iLayNo = Convert.ToInt32(strLayNo.Substring(0, strLayNo.Length - 1));     //层
            DataRowView dv = (DataRowView)e.Row.DataItem;
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                //列号处理  列号从1开始
                //if (i == 0)
                //{
                //    iLcol = 1;
                //}
                //else if (i == 1)
                //{
                //    iLcol = 1;
                //}
                //else
                //{
                //    iLcol = i-1;
                //}

                //列的数据获取处理  列从2开始
                if (i < 2)
                {
                    iLcol = 1;
                }
                else
                {
                    iLcol = i - 1;
                }


                //参数 （巷道，排，列，层）
                //e.Row.Cells[i].Attributes.Add("ondblclick", "showEdit(" + ddlNo_Lane.SelectedValue.ToString() + "," + e.Row.Cells[0].Text.ToString() + ",'" + iLcol + "'," + e.Row.Cells[1].Text.ToString() + ")");
                //e.Row.Cells[i].Attributes.Add("onMouseOver", "SetNewColor(this);");
                //e.Row.Cells[i].Attributes.Add("onMouseOut", "SetNewColor1(this);");

                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
                e.Row.ToolTip = "Click to select row";

                e.Row.Cells[i].Attributes["onclick"] = "showEdit(" + lbl_LaneNo + "," + e.Row.Cells[0].Text.ToString().Substring(0, 1) + "," + iLcol + "," + cid + "," + rid + ");";

            }
        }
    }
    protected void ibtnAddKongpan_Click(object sender, ImageClickEventArgs e)
    {
        string Infor = "";
        int SucessNum = 0;
        int FailNum = 0;
        int subArea = int.Parse(ddlNo_Col.SelectedValue);
        try
        {
            if (txtELcol.Text == "" || txtELine.Text == "" || txtSLcol.Text == "" || txtSLine.Text == "")
            {
                //ShowBox.MessageBox(this.Page, "请选择货位号！");
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "提示", "alert(' 请选择货位号!')", true);
                return;
            }
            else
            {
                for (int i = Convert.ToInt32(txtSLine.Text); i <= Convert.ToInt32(txtELine.Text); i++)
                {
                    for (int j = Convert.ToInt32(txtSLcol.Text); j <= Convert.ToInt32(txtELcol.Text); j++)
                    {
                        lbl_LaneNo = ddlNo_Lane.SelectedValue.ToString();       //巷道号
                        iLineNo = Convert.ToInt32(GV.Rows[i - 1].Cells[0].Text.Substring(0, 1));     //排
                        iLcol = subArea == 1 ? j : (colArea + j);         //列
                                           //iLayNo = Convert.ToInt32(GV.Rows[i - 1].Cells[1].Text.Substring(0, 1));     //层
                        string strLayNo = GV.Rows[i - 1].Cells[1].Text;
                        iLayNo = Convert.ToInt32(strLayNo.Substring(0, strLayNo.Length - 1));

                        cmd = access.GetStoredProcCommond("SP_ADD_And_Del_SpaceTray");
                        access.AddInParameter(cmd, "@Lane", DbType.Int16, Convert.ToInt16(lbl_LaneNo.ToString()));
                        access.AddInParameter(cmd, "@Row", DbType.Int16, iLineNo);
                        access.AddInParameter(cmd, "@Col", DbType.Int16, Convert.ToInt32(iLcol.ToString()));
                        access.AddInParameter(cmd, "@Layer", DbType.Int16, iLayNo);

                        access.AddInParameter(cmd, "@Operate", DbType.Int32, 1);

                        access.AddOutParameter(cmd, "@ClearNO", DbType.Int16, 100);
                        access.AddOutParameter(cmd, "@ResNO", DbType.Int16, 100);
                        access.AddOutParameter(cmd, "@ErrMsg", DbType.String, 255);
                        access.ExecuteNonQuery(cmd);

                        int iResNo = Convert.ToInt32(access.GetParameter(cmd, "@ResNO").Value.ToString());
                        string strErrMsg = access.GetParameter(cmd, "@ErrMsg").Value.ToString();

                        if (iResNo < 0) //失败
                        {
                            FailNum++;
                            Infor += strErrMsg + ";";
                        }
                        else
                        {
                            SucessNum++;
                        }
                    }
                }

                lab_Msg.Text = "成功执行：" + SucessNum + ";失败了:" + FailNum + ";" + Infor + "";
                bind();
            }
            txtELcol.Text = "";
            txtELine.Text = "";
            txtSLcol.Text = "";
            txtSLine.Text = "";
        }
        catch
        {
            lab_Msg.Text = "添加空托盘异常跳出";
        }
    }



    protected void ibtnDelKongpan_Click(object sender, ImageClickEventArgs e)
    {
        string Infor = "";
        int SucessNum = 0;
        int FailNum = 0;
        int subArea = int.Parse(ddlNo_Col.SelectedValue);
        try
        {
            if (txtELcol.Text == "" || txtELine.Text == "" || txtSLcol.Text == "" || txtSLine.Text == "")
            {
                //ShowBox.MessageBox(this.Page, "请选择货位号！");
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "提示", "alert(' 请选择货位号!')", true);
                return;
            }
            else
            {
                for (int i = Convert.ToInt32(txtSLine.Text); i <= Convert.ToInt32(txtELine.Text); i++)
                {
                    for (int j = Convert.ToInt32(txtSLcol.Text); j <= Convert.ToInt32(txtELcol.Text); j++)
                    {
                        lbl_LaneNo = ddlNo_Lane.SelectedValue.ToString();       //巷道号
                        iLineNo = Convert.ToInt32(GV.Rows[i - 1].Cells[0].Text.Substring(0, 1));     //排
                        iLcol = subArea == 1 ? j : (colArea + j);         //列
                                           //iLayNo = Convert.ToInt32(GV.Rows[i - 1].Cells[1].Text.Substring(0, 1));     //层
                        string strLayNo = GV.Rows[i - 1].Cells[1].Text;
                        iLayNo = Convert.ToInt32(strLayNo.Substring(0, strLayNo.Length - 1));

                        cmd = access.GetStoredProcCommond("SP_ADD_And_Del_SpaceTray");
                        access.AddInParameter(cmd, "@Lane", DbType.Int16, Convert.ToInt16(lbl_LaneNo.ToString()));
                        access.AddInParameter(cmd, "@Row", DbType.Int16, iLineNo);
                        access.AddInParameter(cmd, "@Col", DbType.Int16, Convert.ToInt32(iLcol.ToString()));
                        access.AddInParameter(cmd, "@Layer", DbType.Int16, iLayNo);

                        access.AddInParameter(cmd, "@Operate", DbType.Int32, 0);  //删除
                        access.AddOutParameter(cmd, "@ClearNO", DbType.Int16, 100);
                        access.AddOutParameter(cmd, "@ResNO", DbType.Int16, 100);
                        access.AddOutParameter(cmd, "@ErrMsg", DbType.String, 255);
                        access.ExecuteNonQuery(cmd);

                        int iResNo = Convert.ToInt32(access.GetParameter(cmd, "@ResNO").Value.ToString());
                        string strErrMsg = access.GetParameter(cmd, "@ErrMsg").Value.ToString();

                        if (iResNo < 0) //失败
                        {
                            FailNum++;
                            Infor += strErrMsg + ";";
                        }
                        else
                        {
                            SucessNum++;
                        }
                    }
                }

                lab_Msg.Text = "成功执行：" + SucessNum + ";失败了:" + FailNum + ";" + Infor + "";
                bind();
            }
            txtELcol.Text = "";
            txtELine.Text = "";
            txtSLcol.Text = "";
            txtSLine.Text = "";
        }
        catch
        {
            lab_Msg.Text = "删除空托盘异常跳出";
        }
    }

    /// <summary>
    /// 空托盘出库操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnOutSpaceTray_Click(object sender, ImageClickEventArgs e)
    {
        string Infor = "";
        int SucessNum = 0;
        int FailNum = 0;
        int subArea = int.Parse(ddlNo_Col.SelectedValue);
        try
        {
            string strPos_ID = "";
            //if (ddlSQLIDE.SelectedValue == "0")
            //{
            //    lab_Msg.Text = "请选择目的设备号！";
            //    return;
            //    //以下方法：不能使用在局部刷新功能下
            //    //ShowBox.MessageBox(this.Page, "请选择目的设备号！");
            //    //return;
            //    //Response.Write("<script language='javascript'>alert('请选择目的设备号！');</script>");

            //}
            if (txtELcol.Text == "" || txtELine.Text == "" || txtSLcol.Text == "" || txtSLine.Text == "")
            {
                //ShowBox.MessageBox(this.Page, "请选择货位号！");
                //return;
                lab_Msg.Text = "请选择货位号！";
                return;
            }
            else
            {
                // 获取出库口设备编号
                var device = CmdBll.GetDeviceCmdInfo();
                if (device.IsEmpty())
                {
                    ShowBox.MessageBox(this.Page, "出库口设备现在处于禁用或者离线模式,无法下发指令!");
                    return;
                }

                for (int i = Convert.ToInt32(txtSLine.Text); i <= Convert.ToInt32(txtELine.Text); i++)
                {
                    for (int j = Convert.ToInt32(txtSLcol.Text); j <= Convert.ToInt32(txtELcol.Text); j++)
                    {
                        lbl_LaneNo = ddlNo_Lane.SelectedValue.ToString();       //巷道号
                        iLineNo = Convert.ToInt32(GV.Rows[i - 1].Cells[0].Text.Substring(0, 1));     //排
                        iLcol = subArea == 1 ? j : (colArea + j);         //列
                        //iLayNo = Convert.ToInt32(GV.Rows[i - 1].Cells[1].Text.Substring(0, 1));     //层
                        string strLayNo = GV.Rows[i - 1].Cells[1].Text;
                        iLayNo = Convert.ToInt32(strLayNo.Substring(0, strLayNo.Length - 1));     //层

                        strSQL = "SELECT Pos_ID FROM TBase_Position WHERE No_Lane = '" + lbl_LaneNo.ToString() + "' AND No_Line = '" + iLineNo + "' AND No_Col = '" + iLcol + "' AND No_Layer = '" + iLayNo + "'";
                        cmd = access.GetSqlStringCommond(strSQL);
                        ds = access.ExecuteDataSet(cmd);
                        if (ds.Tables[0].Rows.Count < 0)
                        {
                            ShowBox.MessageBox(this.Page, "不存在此货位，请重新选择！");
                            return;
                        }
                        else
                        {
                            strPos_ID = ds.Tables[0].Rows[0]["Pos_ID"].ToString();
                        }
                        cmd = access.GetStoredProcCommond("SP_WMS_Generate_Out_CMDS_ByPos");
                        access.AddInParameter(cmd, "@PosS_ID", DbType.Int64, Convert.ToInt64(strPos_ID.ToString()));
                        access.AddInParameter(cmd, "@SQIDE", DbType.Int16, device.SQID);
                        access.AddInParameter(cmd, "@CommandOrder", DbType.Int16, 0);
                        access.AddInParameter(cmd, "@User_Name", DbType.String, Request.Cookies["vNovaUserNo"].Value.ToString());
                        access.AddOutParameter(cmd, "@ResNO", DbType.Int16, 100);
                        access.AddOutParameter(cmd, "@ErrMsg", DbType.String, 255);
                        access.ExecuteNonQuery(cmd);

                        int iResNo = Convert.ToInt32(access.GetParameter(cmd, "@ResNO").Value.ToString());
                        string strErrMsg = access.GetParameter(cmd, "@ErrMsg").Value.ToString();
                        if (iResNo < 0) //失败
                        {
                            FailNum++;
                            Infor += strErrMsg + ";";
                        }
                        else
                        {
                            SucessNum++;
                        }

                    }
                }


                lab_Msg.Text = "成功执行：" + SucessNum + ";失败了:" + FailNum + ";" + Infor + "";

                bind();

            }
            txtELcol.Text = "";
            txtELine.Text = "";
            txtSLcol.Text = "";
            txtSLine.Text = "";
        }
        catch(Exception ex)
        {
            lab_Msg.Text = "空托盘出库异常跳出";
        }
    }

    protected void ddlNo_Col_SelectedIndexChanged(object sender, EventArgs e)
    {
        bind();
    }
}
